//
//  NewFeatureViewController.swift
//  Weibo
//
//  Created by 孙成宇 on 15/11/29.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit




private let reuseIdentifier = "Cell"
private let maxImageCount = 4

class NewFeatureViewController: UICollectionViewController {

    
    init(){
    
    
    let layout = UICollectionViewFlowLayout()
        
        //设置cell的大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        super.init(collectionViewLayout: layout)
      
        
        collectionView?.pagingEnabled = true
            
        //  collectionView?.pagingEnabled = true
        
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
        
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
   
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
}
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection sectin: Int) -> Int {
        
        return maxImageCount
    }
    override func collectionView(collectionView:
        UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
           
           
            cell.index = indexPath.item
            return cell
            
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.visibleCells().last as!NewFeatureCell
        let index = collectionView.indexPathForCell(cell)
        let indexItem = index?.item ?? 0
        if indexItem == maxImageCount - 1{
        
        
    cell.startAnimation()
        }
    }
    

   }

//自定义cell
class NewFeatureCell : UICollectionViewCell {
    var index: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            
            startBtn.hidden = true
        }
    }

    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    private func setupUI() {
        //添加子视图
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        
        //设置布局
        //snp_makeConstraints  添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            //添加底部约束
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
        
        
    }

    
    func startAnimation() {
    startBtn.hidden = false
   
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
          
            
            }) { (_) -> Void in
                print("OK")
        }
    
    
    
    }
    
    
    @objc private func startBtnDidClick() {
    
    print(__FUNCTION__)
        
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    
    }
    
    
    
    
    
    
    // 懒加载子视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var startBtn: UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: UIColor.whiteColor(), fontSize: 18, isNeedHighlighted: true)


}



